import Gen
from conf import *

def read_file(path):
    f = Gen.file_to_list(path)
    n = Gen.get_2_n(len(DDRPHY_group)*2)
    level_group = []
    asum = 0
    for i in range(n,-1,-1):
        mylist = []
        for j in range(2**i):
            mylist.append(f[asum+j])            
        asum += (2**i)
        level_group.append(mylist)
    return level_group[::-1]

def run(space):
    f = open(FLOW_DDR_ROUTE+"/buffer_point.txt", 'w')
    f2 = open(FLOW_DDR_ROUTE+"/x.txt", 'w')
    f3 = open(FLOW_DDR_ROUTE+"/line_after_placeBUF.txt", 'w')
    f.close()
    f2.close()
    f3.close()
    level_group = read_file(FLOW_DDR_ROUTE+"/routing_point_2")
    
    surplus = 0
    alist = []
    for group in range(len(level_group)):
        blist = []
        print(alist)
        if group == 0:
            for line in level_group[group]:
                blist.append(one_line(line, surplus, space))
        else:
            for line in range(len(level_group[group])):
                if line%2==0:
                    blist.append(one_line(level_group[group][line], alist[-1][int(line//2)], space))
                elif line%2==1:
                    blist.append(one_line(level_group[group][line], alist[-1][int((line-1)//2)], space))
        alist.append(blist)

def one_line(point_list, surplus, space):
    point_list = str_toTuple(point_list)[::-1]
    for point in range(len(point_list)-1):
        if point == 0:
            sur = (space - surplus)/2
            s = get_buffer_point(point_list[point], point_list[point+1], surplus+sur, space)
        else:
            s = get_buffer_point(point_list[point], point_list[point+1], s, space)
    return s

def get_buffer_point(p1, p2, surplus, space):
    f = open(FLOW_DDR_ROUTE+"/buffer_point.txt", 'a')
    f2 = open(FLOW_DDR_ROUTE+"/line_after_placeBUF.txt", 'a')
    alen, dire = get_length(p1, p2)
    all_len = alen+surplus
    f2.write("{} {} ".format(p1[0], p1[1]))
    if all_len >= space:
        xx = int(all_len//space)
        s = all_len-all_len//space*space
        if space-surplus < alen:
            x = space-surplus
            if dire == "up":
                for i in range(xx):
                    f.write("{} {} {}\n".format(p1[0], Gen.round_3(p1[1]+x), "up"))
                    # 记录插入buff后被截断的线段，处理这个数值DDR_ROUTE_BUF_HEIGHT/2+0.2是为防止短路
                    f2.write("{} {}\n".format(p1[0], Gen.round_3(p1[1]+x-DDR_ROUTE_BUF_HEIGHT/2+0.2)))
                    f2.write("{} {} ".format(p1[0], Gen.round_3(p1[1]+x+DDR_ROUTE_BUF_HEIGHT/2-0.2)))
                    print("{} {} {}\n".format(p1[0], Gen.round_3(p1[1]+x), "up"))
                    x += space
            elif dire == "down":
                for i in range(xx):
                    f.write("{} {} {}\n".format(p1[0], Gen.round_3(p1[1]-x), "down"))

                    f2.write("{} {}\n".format(p1[0], Gen.round_3(p1[1]-x+DDR_ROUTE_BUF_HEIGHT/2-0.2)))
                    f2.write("{} {} ".format(p1[0], Gen.round_3(p1[1]-x-DDR_ROUTE_BUF_HEIGHT/2+0.2)))
                    print("{} {} {}\n".format(p1[0], Gen.round_3(p1[1]-x), "down"))
                    x += space
            elif dire == "right":
                for i in range(xx):
                    f.write("{} {} {}\n".format(Gen.round_3(p1[0]+x), p1[1], "right"))

                    f2.write("{} {}\n".format(Gen.round_3(p1[0]+x-DDR_ROUTE_BUF_WIDTH/2+0.2), p1[1]))
                    f2.write("{} {} ".format(Gen.round_3(p1[0]+x+DDR_ROUTE_BUF_WIDTH/2-0.2), p1[1]))
                    print("{} {} {}\n".format(Gen.round_3(p1[0]+x), p1[1], "right"))
                    x += space
            elif dire == "left":
                for i in range(xx):
                    f.write("{} {} {}\n".format(Gen.round_3(p1[0]-x), p1[1], "left"))

                    f2.write("{} {}\n".format(Gen.round_3(p1[0]-x+DDR_ROUTE_BUF_WIDTH/2-0.2), p1[1]))
                    f2.write("{} {} ".format(Gen.round_3(p1[0]-x-DDR_ROUTE_BUF_WIDTH/2+0.2), p1[1]))
                    print("{} {} {}\n".format(Gen.round_3(p1[0]-x), p1[1], "left"))
                    x += space
        else:
            s = all_len
    else:
        s = all_len
    f2.write("{} {}\n".format(p2[0], p2[1]))
    f.close()
    f2.close()
    return Gen.round_3(s) 

def get_length(x1, x2):
    if x1[0] == x2[0]:
        if x1[1] > x2[1]:
            return Gen.round_3(abs(x1[1] - x2[1])), "down"
        if x1[1] <= x2[1]:
            return Gen.round_3(abs(x1[1] - x2[1])), "up"
    if x1[1] == x2[1]:
        if x1[0] > x2[0]:
            return Gen.round_3(abs(x1[0] - x2[0])), "left"
        if x1[0] <= x2[0]:
            return Gen.round_3(abs(x1[0] - x2[0])), "right"
    
def str_toTuple(mylist):
    relist = []
    for i in range(len(mylist)//2):
        mytuple = [float(mylist[2*i]), float(mylist[2*i+1]), 0]
        relist.append(mytuple)
    return relist

run(200)